<?php


namespace app\api\traits;


trait Category
{
    public function category_add()
    {
        $post = $this->request->post();
        $model = new \models\Category();

        $post['moduleid'] = $this->moduleid;
        $post['listorder'] = $model->max_catid() + 1;

        if (!$post['moduleid']) {
            return $this->msg(0, 'mouleid不能为空');
        }

        if (!$post['catname']) {
            return $this->msg(0, '分类名称不能为空');
        }

        $cond = [];
        $cond[] = ['moduleid', '=', $post['moduleid']];
        $cond[] = ['parentid', '=', $post['parentid']];
        $cond[] = ['catname', '=', $post['catname']];
        $e = $model->where($cond)->find();
        if ($e) {
            return $this->msg(0, '分类名称已经存在');
        }

        $insertid = $model->insertGetId($post);
        $model->cache($this->moduleid);
        return $this->msg($insertid);
    }

    public function category_lst()
    {
        $cond = [];
        $cond['moduleid'] = $this->moduleid;

        $model = new \models\Category();
        $category_arr = $model->where($cond)->select();
        $category_arr = $model->getCatTree($category_arr);
        return $this->msg(1, '', '', $category_arr);
    }

    public function category_mod()
    {
        $post = $this->request->post();
        $catid = isset($post['catid']) ? intval($post['catid']) : 0;
        if (!$catid) {
            return $this->msg(0, 'lose catid');
        }
        $model = new \models\Category();
        $post['moduleid'] = $this->moduleid;
        $post['listorder'] = intval($post['listorder']);

        if (!$post['moduleid']) {
            return $this->msg(0, 'mouleid不能为空');
        }

        if (!$post['catname']) {
            return $this->msg(0, '分类名称不能为空');
        }

        $cond = [];
        $cond[] = ['catid', '<>', $catid];
        $cond[] = ['moduleid', '=', $post['moduleid']];
        $cond[] = ['parentid', '=', $post['parentid']];
        $cond[] = ['catname', '=', $post['catname']];
        $e = $model->where($cond)->find();
        if ($e) {
            return $this->msg(0, '分类名称已经存在');
        }

        $r = $model->mod_check_ok($this->moduleid, $catid, $post['parentid']);
        if (!$r) {
            return $this->msg(0, '上级分类选择错误');
        }

        $r = $model->where(['catid' => $catid])->update($post);
        $model->cache($this->moduleid);
        return $this->msg($r === false ? false : true);
    }

    public function category_del()
    {
        $catid = (int)$this->request->post('catid', 0);
        if (!$catid) {
            return $this->msg(0, 'lose catid');
        }
        $model = new \models\Category();
        $r = $model->del_check_ok($catid, $this->moduleid);
        if (!$r) {
            return $this->msg(0, '该分类无法删除，请先删除其子分类，并删除其下文章');
        }
        $cat = $model->where(['moduleid' => $this->moduleid, 'catid' => $catid])->find();
        if (!$cat) {
            return $this->msg(0, '数据不存在');
        }
        $r = $model->where(['moduleid' => $this->moduleid, 'catid' => $catid])->delete();
        $model->cache($this->moduleid);
        return $this->msg($r === false ? false : true);
    }

    public function category_preview()
    {
        $catid = (int)$this->request->post('catid', 0);
        if (!$catid) {
            return $this->msg(0, 'lose catid');
        }
        $model = new \models\Category();
        $data = $model->where(['moduleid' => $this->moduleid, 'catid' => $catid])->find();
        if (!$data) {
            return $this->msg(0, '分类不存在');
        }
        return $this->msg(1, '', '', $data);
    }
}